package org.kpli.jak.db.sec;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author dendy
 * 
 */
public class AddReadPerson {

	public static void main(String[] args) {

		SessionFactory sessionFactory = new Configuration().configure()
				.buildSessionFactory();

		Session session = sessionFactory.openSession();

		session.beginTransaction();

		createPerson(session);
		createQuery(session);
	}

	/**
	 * Fungsi untuk menambahkan data kedalam table didatabase
	 * 
	 * @param session
	 */
	public static void createPerson(Session session) {
		Person person = new Person();
		person.setId(5);
		person.setNama("yogi");
		person.setAlamat("cengkareng");

		session.save(person);
	}

	/**
	 * Membuat qery,commit dan menampilkan
	 * 
	 * @param session
	 */
	@SuppressWarnings("unchecked")
	public static void createQuery(Session session) {
		Query query = session.createQuery("from Person");

		List<Person> list = query.list();

		java.util.Iterator<Person> iter = list.iterator();

		while (iter.hasNext()) {

			Person person = iter.next();
			System.out.println("id " + person.getId());
			System.out.println("nama " + person.getNama());
			System.out.println("alamat " + person.getAlamat());
		}

	}
}
